<?php
	require_once '_common.php';

	class Fmgr0v0r01_file extends Fmgr0v0r01__common implements Fmgr_File{

		var $media_class = "Unknown File";
		
		function set_functions(){
			static $a;
			if(!$a) $a = array_merge(parent::set_functions(), array(
				'template'
			));
			return $a;	
		}
		
		function get_functions(){
			static $a;
			if(!$a) $a = array_merge(parent::get_functions(), array(
				'contents',
				'template'
			));
			return $a;	
		}
		
		function summary_vars(){
			static $a;
			if(!$a) $a = array_merge_recursive(parent::summary_vars(), array(
				"1:" => array(
					'template'
				)
			));
			return $a;
		}
		
		protected $dir = 0;
				
		protected $mod = 644;
		
		protected $usurp_mode = false;
		
		protected $handle;
		
		function __call($func, $params=array()){
			static $fmethods;
			if(!$fmethods){
				$fmethods = rsrcHandle::fmethods();
				array_unshift($fmethods, 'open', 'close');	
			}
			if(!in_array($func, $fmethods)){
				trigger_error(get_class($this).": $func is not a callable method", E_USER_ERROR);
				return;
			}
			if(!$this->handle) $this->handle = new rsrcHandle($this);
			return call_user_func_array(array($this->handle, $func), $params);
		}
		
####	get functions
		
		function forced_xtn(){
			return false;	
		}
		
		function is_draft(){
			return (substr($this->parent()->fmrp, 0, 11) == 'temp:/drafts/');	
		}
		
		function body(){
			if(!$this->allow_read) return;
			$b = $this->get_body();
			if(!$b || !PUBLIC_DOMAIN) return $b;
			$this->data->body = '';
			$b = smuoi_parse($b, $this);
			unset($this->data->body);
			return $b;
		}
		
		function contents(){
			if(!PUBLIC_DOMAIN || (!$t = $this->template())) return '';
			$this->data->contents = '';
			// we have to store My()->caller and restore it after
			$caller = My()->caller;
			My()->caller = $this;
			$c = $t->contents();
			My()->caller = $caller;
			unset($this->data->contents);
			return $c;
		}
		
		function put(){
			if(!$this->allow_read) return;
			header("Content-type: ".$this->data->mime);
			$this->open();
			while($d = $this->read(4096)) echo $d;
			$this->close();
		}
		
		// parent::body always returns an unparsed blob, since file is one class up from _common
		protected function get_body(){
			return (!$this->usurp_mode) ? parent::body() : $this->get_usurpa()->get_ltxt('lstr');
		}
				
		function usurpa(){
			if(!$this->allow_read || (!$u = $this->data->usrp) || (!$u = $this->db()->get($u))) return $this;
			$this->allow_write = !$this->usurp_mode = true;
			foreach(array('title', 'tags', 'description', 'tmpl') as $k) $this->data->$k = $u->data->$k;
			return $this;
		}
		
		function get_usurpa(){
			return $this->db()->get($this->data->usrp);
		}
		
		function template(){
			if(!$this->allow_read) return false;
			if($d = $this->data->get('tmpl')) return $this->db()->get($d);
			if(!PUBLIC_DOMAIN || (document() !== $this)) return false;
			if((!$t = smuoi_getenv_default_template())||(!smuoi_is_text($t))) return false;
			return ($t === $this) ? false : $t;
		}
		
		function join_index($index){
			if(!$this->allow_read) return;
			$index->set($this->id, $this->href());
		}
		
####	set functions
		
		function build_basename(){
			$b = ($c = $this->get_intended('basename')) ? $c : $this->basename();
			return  smuoi_filename($b, $this->forced_xtn());
		}
		
		protected function set_template($id){
			if(!$this->template_loop_test($id)) return false;
			return $this->edits['tmpl'] = $id;
		}
			
		protected function template_loop_test($id){
			do{
				if($id == $this->id) return false;
			}while(($t = $this->db()->get($id)) && ($id = $t->tmpl));
			return true;
		}
		
		protected function set_class($class){
			if(!sm_amiroot()) return false;
			return $this->pkg->is_fileclass($class);	
		}
		
		
####	commital
		
		function copy_to(Fmgr_Directory $par, $lnks=true){
			if(!$this->allow_read || !$par->is_writex()) return false;
			$o = new obj(array('new'=>1, 'dir'=>0));
			if(!$o->srcpath = Cache::tmpCopy($this->realpath())) return false;
			// We need an id now to stick the links in for ltxt
			$o->id = $this->pkg->new_id($this->key());
			foreach($this->cols() as $k){
				if(in_array($k, array('id', 'lstr', 'lobj', 'fmrp', 'basename'))) continue;
				$o->$k = $this->data->get($k);
			}
			//if($lnks && $this->data->lstr) $o->lstr = $this->pkg->Longtext()->duplicate($this->data->lstr, $o->id);
			return $par->appendChild($o, 1);
		}
		
		protected function check($c){
			switch($c){
				case 'filesize':
				case 'checksum':
				case 'mime':
				case 'class':
					error_log($this->fmrp.' checking '.$c);
					if(!$this->exists()) return;
					foreach($this->cold_settings() as $k=>$v){
						($k == 'filesize') ? ($this->edits[$k] = $v) : $this->hard_set($k, $v);						
						unset($this->sanity_checks[$k]);
					}
			}
		}
		
		protected function destroy_row(){
			parent::destroy_row();
			$this->pkg->open('temp:/drafts')->undraft($this);
			return true;
		}

		function destroy(){
			if(!$this->is_destroyable()) return false;
			if($this->exists()) unlink($this->realpath());
			return $this->destroy_row();
		}

	}

?>